﻿namespace Web.Control.WWW.Group
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using Lx.Web.Common.ControlsUI;
    using System.Collections;
    using Model.WWW_WWW.Group;
using System.Collections.Generic;

    /// <summary>
    /// EditGroup 的摘要说明
    /// </summary>
    public class FriendsGroups : BaseControl
    {
        /// <summary>
        /// 当前会员编号
        /// </summary>
        protected string Memberno = "";
        protected List<GroupModel> GMemberList;
        /// <summary>
        /// 绑定组数据
        /// </summary>
        protected DataList gList;        
        /// <summary>
        /// 绑定讨论数据
        /// </summary>
        protected Repeater rptTopic;
        /// <summary>
        /// 数据数量
        /// </summary>
        protected int RecordCount = 0;
        protected int PageIndex = 1;
        protected Literal litCount;

        protected string GP051 = "";
        protected override void OnLoad(EventArgs e)
        {
            Memberno = HttpContext.Current.User.Identity.Name;
            if (Memberno == "")
            {
                Response.Redirect(LoginHost + "/User/login.htm?ReturnUrl=" + Page.Request.Url.AbsoluteUri.Split('?')[0]);
            }
            if (!string.IsNullOrEmpty(Request["p"]))
            {
                if (Request["page"] == "L")
                {
                    PageIndex = 999;
                }
                else
                {
                    int.TryParse(Request["p"], out PageIndex);
                    if (PageIndex == 0)
                    {
                        PageIndex = 1;
                    }
                }
            }
            int psize = 10;
            if (litCount != null) int.TryParse(litCount.Text, out psize);
            ArrayList arrList = BLF.WWW_WWW.Group.Group.GetFriendsGroups(Memberno, PageIndex, psize);
            if (arrList != null && arrList.Count > 2)
            {
                GP051 = GetText("GP051");

                RecordCount = (int)arrList[0];
                int pCount = (int)Math.Ceiling(((decimal)RecordCount) / ((decimal)psize));
                if (PageIndex > pCount) PageIndex = pCount;
                GMemberList = arrList[2] as List<GroupModel>;
                if (gList != null)
                {
                    gList.DataSource = arrList[1];
                    gList.DataBind();
                }                
                if (rptTopic != null)
                {
                    rptTopic.DataSource = arrList[3];
                    rptTopic.DataBind();
                }
            }
            else
            {
                Response.End();
            }
            base.OnLoad(e);
        }

        protected override void OnInit(EventArgs e)
        {
            if (gList != null) gList.ItemDataBound += new DataListItemEventHandler(gList_ItemDataBound);
            
            base.OnInit(e);
        }

        void gList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater c = (Repeater)e.Item.FindControl("rptChild");
                if (c != null)
                {
                    c.DataSource = GetChildData(((List<GroupModel>)((DataList)sender).DataSource)[e.Item.ItemIndex].GID);
                    c.DataBind();
                }
            }
        }

       
        protected List<GroupModel> GetChildData(Int64 gid)
        {
            List<GroupModel> list = new List<GroupModel>();
            foreach (GroupModel m in GMemberList)
            {
                if (m.GID == gid) list.Add(m);
            }
            return list;
        }
    }
}
